package com.supermap.gwfs.message.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.Properties;

public class ConfigReader {
    private static ConfigReader instance;
    private JSONArray windSpeedCode;
    private JSONArray windDirectionCode;
    private Properties FTPServerProperty;
    public static ConfigReader getInstance(){
        if(instance == null){
            instance = new ConfigReader();
        }
        return instance;
    }

    private ConfigReader() {
        readWindSpeedCodeConfig();
        readFTPServerConfig();
    }

    private void readFTPServerConfig() {
        InputStream in = ConfigReader.class.getClassLoader().getResourceAsStream("config/FTPServer.properties");

        try {
            FTPServerProperty = new Properties();
            FTPServerProperty.load(in);
        } catch (IOException e) {
            FTPServerProperty = null;
        }

    }

    /**
     * 读取风速风向编码
     */
    private void readWindSpeedCodeConfig() {
        String path = this.getClass().getClassLoader().getResource("config/wind-code-config.json").getPath();
        StringBuilder content = new StringBuilder();
        String jsonString = "";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
            String str = reader.readLine();
            while (str != null){
                content.append(str);
                str = reader.readLine();
            }
            jsonString = content.toString();
        } catch (IOException e) {
            windSpeedCode = null;
            windDirectionCode = null;
        }
        JSONObject data = JSONObject.parseObject(jsonString).getJSONObject("Wind");
        windSpeedCode = data.getJSONArray("SpeedCode");
        windDirectionCode = data.getJSONArray("DirectionCode");
    }

    public JSONArray getWindSpeedCode() {
        return windSpeedCode;
    }

    public JSONArray getWindDirectionCode() {
        return windDirectionCode;
    }

    public Properties getFTPServerProperty() {
        return FTPServerProperty;
    }
}
